
package doc2db.interfaces.action.InsUpd;

import doc2db.UserProp;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.ComponentOrientation;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

/**
 *
 * @author yura
 */
class TableColumn {
    static JDialog helper = new JDialog();

    private JLabel column_name = new JLabel("колонка:");
    private JLabel columnName;
    private JLabel data_type = new JLabel  ("тип данных:");
    private JLabel dataType;
    private JLabel dataLabel = new JLabel  ("данные:");
    private JTextArea data = new JTextArea();
    private JScrollPane sd = new JScrollPane(data);
    JCheckBox forUpdate = new JCheckBox("связывающие данные", false);
    protected JComboBox dataFromTabelColumn = new JComboBox();

    public TableColumn(String columnName, String dataType){
        this.columnName = new JLabel(columnName);
        this.dataType = new JLabel(dataType.replaceAll("varying", "")
                .replaceAll("null", ""));
        dataFromTabelColumn.setCursor(new Cursor(Cursor.HAND_CURSOR));

        this.setFont();
        this.setSize();
        this.setForInsert();
        this.initFrame();
    }

    private void initFrame(){
        helper.setTitle("менеджер БД");
        helper.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
        helper.setResizable(true);
        Toolkit kit = Toolkit.getDefaultToolkit();
        Dimension screnSize = kit.getScreenSize();
        int screanHeight = screnSize.height ;
        int screanWidth = screnSize.width ;
        helper.setSize(300, 300);
        helper.setLocation((screanWidth - 3000), (screanHeight - 290));
    }

    private void setFont(){
        column_name.setFont(UserProp.paneFont);
        columnName.setFont(UserProp.paneFont);
        data_type.setFont(UserProp.paneFont);
        dataType.setFont(UserProp.paneFont);
        dataLabel.setFont(UserProp.paneFont);
        forUpdate.setFont(UserProp.paneFont);
        dataFromTabelColumn.setFont(UserProp.paneFont);

        data.setFont(UserProp.textFont);
    }

    private void setSize(){
        int height = 50;
        int width = 0;
        width = Math.max(width, column_name.getSize().width);
        width = Math.max(width, columnName.getSize().width);
        width = Math.max(width, data_type.getSize().width);
        width = Math.max(width, dataType.getSize().width);
        width = Math.max(width, dataLabel.getSize().width);
        width = Math.max(width, forUpdate.getSize().width);
        width = Math.max(width, dataFromTabelColumn.getSize().width);
        width = Math.max(width, data.getSize().width);

        sd.setMinimumSize(new Dimension(width, height));
        sd.setPreferredSize(data.getPreferredSize());
        sd.setMaximumSize(data.getMaximumSize());

        data.setLineWrap(true);
    }

    public void setForUpdate(String [] arg){
        //dataFromTabelColumn = new JComboBox(arg);
        dataFromTabelColumn.removeAllItems();
        for (int i = 0; i < arg.length; i++) {
            String s = "" + arg[i];
            if(! s.equals("null"))
                dataFromTabelColumn.addItem(s);
        }
        dataFromTabelColumn.setSelectedIndex(0);
        Dimension d = new Dimension(dataFromTabelColumn.getPreferredSize());
        if (d.width > 170) {
            d.width = 170;
            dataFromTabelColumn.setPreferredSize(d);
        }
        d.width = 170;
        dataFromTabelColumn.setMaximumSize(d);
        dataFromTabelColumn.setVisible(true);
        data.setVisible(false);
        sd.setVisible(false);
    }

    public void setForInsert(){
        dataFromTabelColumn.setVisible(false);
        dataFromTabelColumn.removeAllItems();
        data.setVisible(true);
        sd.setVisible(true);
    }

    public Component getPanel(){
        Box p1 = Box.createVerticalBox();
        p1.add(this.getCompInHorizontLine(column_name));
        p1.add(this.getCompInHorizontLine(columnName));
        p1.add(Box.createVerticalStrut(4));
        p1.add(this.getCompInHorizontLine(data_type));
        p1.add(this.getCompInHorizontLine(dataType));
        p1.add(Box.createVerticalStrut(4));
        p1.add(this.getCompInHorizontLine(forUpdate));
        p1.add(Box.createVerticalStrut(4));
        p1.add(this.getCompInHorizontLine(dataLabel));
        p1.add(this.getCompInHorizontLine(dataFromTabelColumn));

        Box p2 = Box.createVerticalBox();
        p2.add(p1);
        p2.add(Box.createVerticalStrut(1));
        //p2.add(data);
        p2.add(sd);
        p2.add(Box.createVerticalStrut(1));
        p2.setBorder(BorderFactory.createEtchedBorder());

        JPanel p = new JPanel(new BorderLayout());
        p.addMouseListener(new MyMouseListener());
        p.add(p2, BorderLayout.CENTER);

        return p;
    }

    private Component getCompInHorizontLine(Component c){
        Box h = Box.createHorizontalBox();
        h.add(Box.createHorizontalStrut(1));
        h.add(c);
        h.add(Box.createGlue());
        return h;
    }

    public String getText(){
        if(data.isVisible())
            return "" + data.getText().replaceAll("\'", "\\\'");
        else
            return (""+dataFromTabelColumn.getSelectedItem()).replaceAll("\'", "\\\'");
    }

    public String getColumnName(){
        return columnName.getText().replaceAll("\"", "\\\"");
    }

    class MyMouseListener extends MouseAdapter {
        @Override
        public void mouseClicked(MouseEvent arg0) {
            super.mouseClicked(arg0);
            if (arg0.getClickCount() == 2) {
                helper.setVisible(false);
                JTextArea text = new JTextArea();
                StringBuffer t = new StringBuffer();
                t.append("" + column_name.getText() + "\n");
                t.append(columnName.getText() + "\n\n");
                t.append(data_type.getText() + "\n");
                t.append(dataType.getText() + "\n\n");
                if (dataFromTabelColumn.isVisible()) {
                    t.append(dataLabel.getText() + "\n");
                    for (int i = 0; i < dataFromTabelColumn.getItemCount(); i++) {
                        t.append( "  " + (i+1) + ") " + dataFromTabelColumn.getItemAt(i) + "\n\n");
                    }
                }
                text.setText(t.toString());
                text.setLineWrap(false);
                text.setEditable(false);
                text.setFont(data.getFont());

                Box p1 = Box.createVerticalBox();
                p1.add(new JScrollPane(text));
                JPanel p = new JPanel(new BorderLayout());
                p.add(p1, BorderLayout.CENTER);
                helper.setContentPane(p);
                helper.setVisible(true);
            }
        }
    }
}
